package com.bj58.analysis.tools;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.bj58.analysis.tools.common.ActionDataUtils;
import com.bj58.analysis.tools.common.GetObjectToClassUtils;
import com.bj58.analysis.tools.common.URLDecodeUtils;
import com.bj58.analysis.tools.common.bean.ActionData;
import com.bj58.analysis.tools.common.bean.LogBean;
import com.bj58.analysis.tools.etl.bean.ETLActionBean;

/**
 * @description 对action ETL后的日志进行解析封装
 * @author tzc
 *
 */
public class ETLActionAnalysis extends CommonAnalysis {


	@Override
	protected LogBean analysis(String logLine) {
		ETLActionBean bean = null;
		if (StringUtils.isNotBlank(logLine)) {
			String[] lines = logLine.split("\001", -1);
			bean = (ETLActionBean) GetObjectToClassUtils.getObjectByArray(
					ETLActionBean.class, lines);
			if (bean != null) {

				// referer
				String referer = bean.getReferrer();
				if (StringUtils.isNotBlank(referer)) {
					String refDomain = URLDecodeUtils.getSearchEngines(referer);
					if (StringUtils.isNotBlank(refDomain)) {
						// refdomain
						bean.setRefDomain(refDomain);
					}
					//clickId
					Map<String, String> map = URLDecodeUtils
					.parserCommonParams(referer);
					if (map != null && map.size() != 0) {	
						String clickId = map.get("ClickID");
						if (StringUtils.isNotBlank(clickId))
							bean.setClickId(clickId);
					}
				}
					
				
				String trackURL = bean.getTrackUrl();
				if (StringUtils.isNotBlank(trackURL)) {
					JSONObject obj = null;
					try {
						obj = JSONObject.fromObject(trackURL);
					} catch (Exception e) {
						e.printStackTrace();
					}// 以上解析方式能正常解析99%的数据，对于极少的个别错误数据直接抛弃trackurl

					if (obj != null) {
						// init_refer
						if (obj.containsKey("init_refer")) {
							String init_refer = obj.getString("init_refer");
							if (StringUtils.isNotBlank(init_refer)) {
								init_refer = URLDecodeUtils
										.parser(init_refer);
								if (StringUtils.isNotBlank(init_refer))
									obj.put("init_refer", init_refer);
							}
						}

						// GTID
						if (obj.containsKey("GTID")) {
							String gTID = obj.getString("GTID");
							if (StringUtils.isNotBlank(gTID))
								bean.setGTID(gTID);
						}

						bean.setTrackUrl(obj.toString());
					}
				}
				
				String actionDataListLine = bean.getActionDataStr();
				if (StringUtils.isNotBlank(actionDataListLine)) {
					String[] actionDataListLines = actionDataListLine.split(
							",", -1);
					List<ActionData> list = new ArrayList<ActionData>();
					for (String actionDataLine : actionDataListLines) {
						ActionData actionData = ActionDataUtils
								.parserToActionData(actionDataLine);
						if (actionData != null)
							list.add(actionData);
					}
					if (list != null && list.size() != 0)
						bean.setActionDataList(list);
				}
								

				// url
				String url = bean.getUrl();
				if (StringUtils.isNotBlank(url)) {

					// PGTID
					String pGTID = URLDecodeUtils.getUrlOfParam(url, "PGTID");
					if (StringUtils.isNotBlank(pGTID))
						bean.setGTID(pGTID);

					// pClickID
					String pClickID = URLDecodeUtils.getUrlOfParam(url,
							"ClickID");
					if (StringUtils.isNotBlank(pClickID))
						bean.setpClickId(pClickID);
				}

			}

		}
		return bean;
	}


	
}
